package com.hl.db_lib.compiler;

import com.google.auto.service.AutoService;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;


@AutoService(Processor.class)
@SupportedAnnotationTypes(value = {
        "com.hl.db_lib.annotation.operate.Delete",
        "com.hl.db_lib.annotation.operate.Update",
        "com.hl.db_lib.annotation.operate.Insert",
        "com.hl.db_lib.annotation.operate.Query",
        "com.hl.db_lib.annotation.select.Where",
        "com.hl.db_lib.annotation.select.WhereOr"
})
public class DBCompiler extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        //找到所有标记有Delete、Insert、Query、Update注解的方法
//        List<Class<? extends TypeElement>> clazzs = new ArrayList<>()
        //从方法中提取where的条件

        //生成查询语句

        return false;
    }

    private void scan(RoundEnvironment env,
                      Class<? extends Annotation> annotation,
                      AnnotationValueVisitor visitor) {

        for (Element typeElement : env.getElementsAnnotatedWith(annotation)) {
            final List<? extends AnnotationMirror> annotationMirrors =
                    typeElement.getAnnotationMirrors();

            for (AnnotationMirror annotationMirror : annotationMirrors) {
                final Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues
                        = annotationMirror.getElementValues();

                for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
                        : elementValues.entrySet()) {


                    entry.getValue().accept(visitor, null);
                }
            }
        }
    }





}
